package com.ita.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oracle.net.aso.s;

import com.ita.dao.BookDao;
import com.ita.dao.impl.BookDaoImpl;
import com.ita.pojo.Book;
import com.ita.pojo.Order;
import com.ita.pojo.User;

public class AddToCartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		Integer bookid = Integer.parseInt(id);
		HttpSession session = request.getSession();

		Map<Integer, Integer> map = (Map<Integer, Integer>) session
				.getAttribute("map");
		if (map == null) {
			map = new HashMap<Integer, Integer>();
			map.put(bookid, 1);
		} else {
			if (map.containsKey(bookid)) {
				Integer num = map.get(bookid);
				num++;
				map.remove(id);
				map.put(bookid, num);
			} else {
				map.put(bookid, 1);
			}
		}
		session.setAttribute("map", map);
		request.getRequestDispatcher("booklist").forward(request, response);
	}

}
